package org.nowcoder.huawei.c100;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;

public class HC011 {
    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String[] s = br.readLine().split(" ");
            int[] nodes = new int[s.length];
            for (int i = 0; i < s.length; i++) {
                nodes[i] = Integer.parseInt(s[i]);
            }
            solve(nodes);
        } catch (Exception ignore) {

        }
    }


    public static void solve(int[] nodes) {
        int sum = 0;
        Queue<Integer> que = new ArrayDeque<>();
        que.offer(0);
        while (!que.isEmpty()) {
            int size = que.size();
            int max = Integer.MIN_VALUE;
            for (int i = 0; i < size; i++) {
                int v = que.poll();
                max = Math.max(max, nodes[v]);

                int l = 2 * v + 1;
                int r = 2 * v + 2;
                if (l < nodes.length && nodes[l] != -1) {
                    que.offer(l);
                }
                if (r < nodes.length && nodes[r] != -1) {
                    que.offer(r);
                }
            }
            sum += max;
        }
        System.out.println(sum);
    }
}
